constraint-editor: Fix creating constant constraints
authorMatthias Clasen <mclasen@redhat.com>
Wed, 18 Nov 2020 04:15:51 +0000 (23:15 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Wed, 18 Nov 2020 04:16:57 +0000 (23:16 -0500)
We were not making the button sensitive in the case
of a constant constraint, and we were not properly
creating constant constraints either.

demos/constraint-editor/constraint-editor-window.c
demos/constraint-editor/constraint-editor.c
demos/constraint-editor/constraint-editor.ui

index 2ec0f4cd6781f30d48330d3ef3da82892d203e10..ca18e27cf8e9e771710eb8604fc05d9b617ecc16 100644 (file)
@@ -36,7 +36,7 @@ struct _ConstraintEditorWindow
 G_DEFINE_TYPE(ConstraintEditorWindow, constraint_editor_window, GTK_TYPE_APPLICATION_WINDOW);
 
 static GtkConstraintTarget *
-find_target (GListModel *model,
+find_target (GListModel          *model,
              GtkConstraintTarget *orig)
 {
   const char *name;
@@ -163,20 +163,30 @@ constraint_editor_window_load (ConstraintEditorWindow *self,
       GtkConstraint *clone;
       GtkConstraintTarget *target;
       GtkConstraintTarget *source;
+      GtkConstraintAttribute source_attr;
 
       item = g_list_model_get_item (list, i);
       constraint = GTK_CONSTRAINT (item);
 
       target = gtk_constraint_get_target (constraint);
       source = gtk_constraint_get_source (constraint);
-      clone = gtk_constraint_new (find_target (constraint_view_get_model (CONSTRAINT_VIEW (self->view)), target),
+      source_attr = gtk_constraint_get_source_attribute (constraint);
+
+      if (source == NULL && source_attr == GTK_CONSTRAINT_ATTRIBUTE_NONE)
+        clone = gtk_constraint_new_constant (find_target (constraint_view_get_model (CONSTRAINT_VIEW (self->view)), target),
                                   gtk_constraint_get_target_attribute (constraint),
                                   gtk_constraint_get_relation (constraint),
-                                  find_target (constraint_view_get_model (CONSTRAINT_VIEW (self->view)), source),
-                                  gtk_constraint_get_target_attribute (constraint),
-                                  gtk_constraint_get_multiplier (constraint),
                                   gtk_constraint_get_constant (constraint),
                                   gtk_constraint_get_strength (constraint));
+      else
+        clone = gtk_constraint_new (find_target (constraint_view_get_model (CONSTRAINT_VIEW (self->view)), target),
+                                    gtk_constraint_get_target_attribute (constraint),
+                                    gtk_constraint_get_relation (constraint),
+                                    find_target (constraint_view_get_model (CONSTRAINT_VIEW (self->view)), source),
+                                    source_attr,
+                                    gtk_constraint_get_multiplier (constraint),
+                                    gtk_constraint_get_constant (constraint),
+                                    gtk_constraint_get_strength (constraint));
 
       constraint_view_add_constraint (CONSTRAINT_VIEW (self->view), clone);
 
index ffbc6a15b03f05712f913b5b62a472bea3f329f4..6f897b2d8a031ee0a7e6fc851fd2662bb5a9f0b3 100644 (file)
@@ -142,6 +142,9 @@ get_target (GListModel *model,
 {
   int i;
 
+  if (id == NULL)
+    return NULL;
+
   if (strcmp ("super", id) == 0)
     return NULL;
 
@@ -347,6 +350,7 @@ source_attr_changed (ConstraintEditor *editor)
     {
       gtk_widget_set_sensitive (editor->source, TRUE);
       gtk_widget_set_sensitive (editor->multiplier, TRUE);
+      gtk_editable_set_text (GTK_EDITABLE (editor->multiplier), "1");
     }
 }
 
@@ -457,8 +461,12 @@ update_preview (ConstraintEditor *editor)
 static void
 update_button (ConstraintEditor *editor)
 {
-  if (gtk_combo_box_get_active_id (GTK_COMBO_BOX (editor->target)) != NULL &&
-      gtk_combo_box_get_active_id (GTK_COMBO_BOX (editor->source)) != NULL)
+  const char *target = gtk_combo_box_get_active_id (GTK_COMBO_BOX (editor->target));
+  const char *source = gtk_combo_box_get_active_id (GTK_COMBO_BOX (editor->source));
+  const char *source_attr = gtk_combo_box_get_active_id (GTK_COMBO_BOX (editor->source_attr));
+
+  if (target &&
+      (source || (source_attr && get_target_attr (source_attr) == GTK_CONSTRAINT_ATTRIBUTE_NONE)))
     gtk_widget_set_sensitive (editor->button, TRUE);
   else
     gtk_widget_set_sensitive (editor->button, FALSE);
index d7c0d0e25be2d4b5629d58ef948becd8dfc019f8..d201aae350fefc3b0197ede3181220c56600363a 100644 (file)
@@ -78,6 +78,7 @@
           <object class="GtkComboBoxText" id="source_attr">
             <signal name="changed" handler="update_preview" swapped="yes"/>
             <signal name="changed" handler="source_attr_changed" swapped="yes"/>
+            <signal name="changed" handler="update_button" swapped="yes"/>
             <layout>
               <property name="column">2</property>
               <property name="row">3</property>